package cn.bdqn.controller;

import cn.bdqn.domain.Driver;
import cn.bdqn.service.DriverService;
import cn.bdqn.utils.result.Page;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 司机控制层
 * @author shaojie
 */
@RestController
public class DriverController {

    @Autowired
    private DriverService driverService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    // ------------------- 服务熔断-----------------
    @GetMapping(value = "/driver/queryByPrimaryKey/{id}")
    public Result queryDriverByPrimaryKey(@PathVariable("id") Integer id){
        try {
            return ResultGenerator.genSuccessResult(driverService.queryDriverByPrimaryKey(id));
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败");
        }
    }

    /**
     * 选择更新信息
     * @param driver
     */
    // ------------------- 服务熔断-----------------
    @RequestMapping(value = "/driver/updateByPrimaryKeySelective/driver")
    public Result updateByPrimaryKeySelective(@RequestBody Driver driver){
        try {
            driverService.updateByPrimaryKeySelective(driver);
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("更新失败");
        }
    }

    /**
     * 多条件查询   分页
     * @param driver
     * @return
     */
    // ------------------- 服务熔断-----------------
    @GetMapping(value = "/driver/queryByDriver/driver/pageNum/{pageNum}/pageAmount/{pageAmount}")
    public Result queryByDriver(@RequestBody Driver driver,@PathVariable("pageNum") Integer pageNum,@PathVariable("pageAmount") Integer pageAmount) {
       try {
            Page page = driverService.queryByDriver(driver,pageNum,pageAmount);
            return ResultGenerator.genSuccessListResult(page.getMenuList(),page.getTotalRecord());
       }catch (Exception e){
           e.printStackTrace();
           return ResultGenerator.genFailResult("查寻失败");
       }
    }

}
